#!/usr/bin/env python3
import argparse
import socket
from scapy.all import Ether, IP, sendp, get_if_hwaddr, get_if_list

def get_interface():
    """获取第一个以太网接口"""
    for iface in get_if_list():
        if "eth0" in iface:
            return iface
    print("错误：未找到eth0接口")
    exit(1)

def main():
    parser = argparse.ArgumentParser(description='发送带ECN标记的测试包')
    parser.add_argument('dst_ip', help='目的IP地址')
    parser.add_argument('--ecn', type=int, default=1, choices=[1,2],
                      help='ECN初始标记（1=01, 2=10，支持ECN）')
    args = parser.parse_args()

    iface = get_interface()
    dst_ip = socket.gethostbyname(args.dst_ip)
    src_mac = get_if_hwaddr(iface)

    # 构建带ECN标记的IPv4包
    pkt = Ether(src=src_mac, dst='ff:ff:ff:ff:ff:ff') / \
          IP(dst=dst_ip, ttl=64, ecn=args.ecn) / \
          "ECN测试包"

    print(f"从接口 {iface} 发送包到 {dst_ip}，ECN标记={args.ecn}")
    pkt.show()
    sendp(pkt, iface=iface, verbose=False)

if __name__ == '__main__':
    main()